package com.ys.peony.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;

import com.ys.peony.app.BaseApplication;
import com.ys.peony.app.HttpURL;
import com.ys.peony.app.YSConstant;
import com.ys.peony.http.JCHttpManagerUtils;
import com.ys.peony.http.base.BaseRespCallBack;
import com.ys.peony.model.UserModel;


/**
 * @Description:缓存管理
 * @Author:yumeili
 * @Since:2016-5-16下午4:15:18
 */
public class SharePreferencesUtils {
    private final static String TAG = "SharePreferencesUtils";

    private static SharePreferencesUtils instance;
    private static Context context;
    private UserModel mUserModel = null;
    private SharePreferencesUtils(Context context) {
        this.context = context;
    }

    public synchronized static SharePreferencesUtils getInstance() {
        if (instance == null) {
            instance = new SharePreferencesUtils(BaseApplication.getInstance());
        }
        return instance;
    }

    private SharedPreferences.Editor getEditor() {
        return getSharedPreferences().edit();
    }

    private static SharedPreferences getSharedPreferences() {
        return PreferenceManager.getDefaultSharedPreferences(context);
    }

    /* 网络获得的数据 */
    public String getValueByKey(String key) {
        return getSharedPreferences().getString(key, "");
    }

    public long getLongValueByKey(String key) {
        return getSharedPreferences().getLong(key,0);
    }

    public void removeKey(String key) {
        SharedPreferences.Editor editor = getEditor();
        editor.remove(key);
        editor.commit();
    }

    public synchronized void setKeyValue(String key, String value) {
        SharedPreferences.Editor editor = getEditor();
        editor.putString(key, value);
        editor.commit();
    }
    public synchronized void setLongValue(String key, long value) {
        SharedPreferences.Editor editor = getEditor();
        editor.putLong(key, value);
        editor.commit();
    }

    /**
     * 设置是否第一次进入主界面
     * 判断首次登录
     */
    public boolean setFirstEnter(boolean isFirstEnter) {
        SharedPreferences.Editor editor = getEditor();
        editor.putBoolean("KEY_FIRST_ENTER", isFirstEnter);
        return editor.commit();
    }

    /**
     * 是否是第一次进入主界面 true 第一次进入主界面
     *
     * @return
     */
    public boolean isFirstEntry() {
        return getSharedPreferences().getBoolean("KEY_FIRST_ENTER", true);
    }

    /**
     * 判断桌面快捷方式是否生成
     * 设置是否第一次加载
     */
    public boolean setFirstLauncher(boolean isFirstEnter) {
        SharedPreferences.Editor editor = getEditor();
        editor.putBoolean("KEY_FIRST_LAUNCHER", isFirstEnter);
        return editor.commit();
    }

    /**
     * 判断桌面快捷方式是否生成
     * 是否是第一次加载
     *
     * @return
     */
    public boolean isFirstLauncher() {
        return getSharedPreferences().getBoolean("KEY_FIRST_LAUNCHER", true);
    }


    /**
     * 保存usertoken
     *
     * @param usertoken
     */
//    public void saveUserToken(String usertoken) {
//        setKeyValue(YSConstant.SP_USERTOKEN, usertoken);
//    }

    /**
     * 获取usertoken
     *
     * @return
     */
    public String getSessionId() {
        String userToken = "";
        UserModel userModel = getUserModel();
        if (userModel != null && userModel.getSessionId() != null) {
            userToken = userModel.getSessionId();
        }
        return userToken;
    }

    /**
     * 获取userId
     *
     * @return
     */
    public String getUserId() {
        UserModel userModel = getUserModel();
        if(userModel != null) {
            Log.d("userId--->", userModel.getId());
        } else {
            Log.d("userId--->", "0");
        }
        return userModel == null ? "0" : userModel.getId();
    }

    /**
     * describe:获取用户信息
     * author:zhangcong
     * date:2017/5/27 17:19
     */
    public UserModel getUserModel() {
        if(mUserModel != null) {
            return mUserModel;
        } else {
            String userString = getValueByKey(YSConstant.USER_INFO);
            if (TextUtils.isEmpty(userString)) {
                return null;
            }
            mUserModel = JSONUtil.parseObject(userString, UserModel.class);
            return mUserModel;
        }
    }

    public void setUserModel(UserModel userModel) {
        mUserModel = userModel;
    }

    public String getUrl() {
        String baseUrl=getSharedPreferences().getString(YSConstant.SP_HTTP_BASEURL, HttpURL.BASE_URL_DEFAULT);
        if(StringUtils.isEmpty(baseUrl)){
            baseUrl=HttpURL.BASE_URL_DEFAULT;
        }
        return baseUrl;
    }


}
